package com.helen.ui.newui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.helen.adapter.MyRecoveryDetailListAdapter;
import com.helen.db.UserHelper;
import com.helen.entity.MyRecoveryDetailEntity;
import com.helen.global.Constants;
import com.helen.shopping.R;
import com.helen.ui.BaseActivity;
import com.helen.utils.MyLog;
import com.helen.utils.PhoneUtils;
import com.helen.utils.tiputils.MToast;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by yang on 2019/3/25
 */
public class MyRecoveryCarDetailActivity extends BaseActivity {
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefresh;
    private List<MyRecoveryDetailEntity> list = new ArrayList();
    private MyRecoveryDetailListAdapter myRecoveryDetailListAdapter;
    private View errorView;
    private int page = 1;
    private int cardId;
    private String name;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_my_recovery_car_detail;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        cardId = getIntent().getIntExtra("carId", 0);
        name = getIntent().getStringExtra("name");
        getToolbarTitle().setText(name);
        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) rvList.getParent(), false);
        myRecoveryDetailListAdapter = new MyRecoveryDetailListAdapter(list);
        swipeRefresh.setRefreshing(true);
        swipeRefresh.setColorSchemeColors(getResources().getColor(R.color.appThemeColor));
        rvList.setLayoutManager(new LinearLayoutManager(MyRecoveryCarDetailActivity.this));
        rvList.setAdapter(myRecoveryDetailListAdapter);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        myRecoveryDetailListAdapter.setOnItemPaoListener(new MyRecoveryDetailListAdapter.OnSetItemPaoListener() {
            @Override
            public void setOnPaoListener(MyRecoveryDetailEntity myRecoveryDetailEntity, int position) {
                paoDialog(myRecoveryDetailEntity.getId()+"",position);
            }
        });
        myRecoveryDetailListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        }, rvList);

        refresh();
    }

    @Override
    protected void updateViews() {

    }

    private void refresh() {
        page = 1;
        getData();
    }


    private void loadMore() {
        page++;
        getData();
    }

    private void getData() {
        mMProgressDialog.show();
        HttpParams params = new HttpParams();
        params.put("userId", UserHelper.getInstance().getloginEntity(MyRecoveryCarDetailActivity.this).getId());
        params.put("carId", cardId + "");
        params.put("page", page + "");
        params.put("size", "10");
        EasyHttp.post(Constants.API_JSONAPI_MY_CAR_LIST_USER)
                .params(params)
                .execute(new SimpleCallBack<String>() {

                    @Override
                    public void onError(ApiException e) {
                        mMProgressDialog.dismiss();
                        swipeRefresh.setRefreshing(false);
                        MToast.makeTextShort(MyRecoveryCarDetailActivity.this, getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(String s) {
                        MyLog.d("yang", "s==" + s);
                        mMProgressDialog.dismiss();
                        swipeRefresh.setRefreshing(false);
                        String data = PhoneUtils.checkResponseFlag2(MyRecoveryCarDetailActivity.this, s, TAG);
                        if (data != null) {
                            list = new Gson().fromJson(data, new TypeToken<List<MyRecoveryDetailEntity>>() {
                            }.getType());
                            if (page == 1) {
                                myRecoveryDetailListAdapter.setNewData(list);
                            } else {
                                myRecoveryDetailListAdapter.addData(list);
                            }
                            if (list.size() < 10) {
                                myRecoveryDetailListAdapter.loadMoreEnd(false);
                                myRecoveryDetailListAdapter.setEmptyView(errorView);
                            } else {
                                myRecoveryDetailListAdapter.loadMoreComplete();
                            }

                        }
                    }
                });

    }


    private void paoDialog(final String id, final int position){
        AlertDialog.Builder builder = new AlertDialog.Builder(MyRecoveryCarDetailActivity.this);
        builder.setTitle("温馨提示")
                .setMessage("确定立即抛弃吗?")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        paoCarApi(id,position);

                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        builder.create().show();
    }
    private void paoCarApi(String id, final int position) {
        HttpParams params = new HttpParams();
        params.put("userId",UserHelper.getInstance().getloginEntity(MyRecoveryCarDetailActivity.this).getId());
        params.put("id",id);
        EasyHttp.post(Constants.API_JSONAPI_PAO_CAR)
                .params(params)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mMProgressDialog.dismiss();
                        swipeRefresh.setRefreshing(false);
                        MToast.makeTextShort(MyRecoveryCarDetailActivity.this, getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(String result) {
                        MyLog.d("yang", "s==" + result);
                        mMProgressDialog.dismiss();
                        swipeRefresh.setRefreshing(false);
                        String data = PhoneUtils.checkResponseFlag(MyRecoveryCarDetailActivity.this, result, TAG);
                        if (data != null) {
                            myRecoveryDetailListAdapter.getData().remove(position);
                            myRecoveryDetailListAdapter.notifyDataSetChanged();
                            MToast.makeTextShort(MyRecoveryCarDetailActivity.this, "抛弃成功").show();
                        }
                    }
                });

    }
}
